//一个状态模块就是一个reducer ,reducer中有定义全局状态,也有修改状态的方法

import { ADD,ADDNUM,REDUCE,REDUCENUM } from '../types'

// 2.定义一个 reducer 纯函数
//该函数中有两个参数
//参数一就是 state ,该state就是全局状态中的数据
//参数二 actions 中有两个属性, 第一个type 标记我们要做的事情 payload 就是参数
const reducer = (
    state = {
      count: 111,
    },
    action
  ) => {
    const { type, payload } = action;
  
    // console.log(type, payload);
  
    switch (type) {
      case ADD:
        return { ...state, count: state.count + 1 };
  
      case REDUCE:
        return { ...state, count: state.count - 1 };
  
      case ADDNUM:
        return { ...state, count: state.count + payload };
  
      case REDUCENUM:
        return { ...state, count: state.count - payload };

      default:
        return state
    }
  };




export default reducer